package com.buka.goods.service.impl;

import com.buka.goods.mapper.ClassifyMapper;
import com.buka.goods.mapper.ProductMapper;
import com.buka.goods.service.DetailService;
import com.buka.goods.service.ProductDetailsService;
import com.buka.goods.service.SpecService;
import com.buka.server.pojo.Classify;
import com.buka.server.pojo.GoodsSpec;
import com.buka.server.pojo.Product;
import com.buka.server.pojo.ProductDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DetailServiceImpl implements DetailService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ClassifyMapper classifyMapper;
    @Autowired
    private ProductDetailsService productDetailsService;
    @Autowired
    private SpecService specService;

    @Override
    public Map<String, ?> findDetail(Long id) {
        Map result = new HashMap();
        //根据id查询商品对象
        Product products = new Product();
        products.setId(id);
        products.setIsDelete(0);
        Product product = productMapper.selectOne(products);
        Classify classify = this.findClassify(product.getClassifyId());
        //分类对象名称
        StringBuilder classifyName = new StringBuilder();
        if (classify != null) {
            classifyName.append(classify.getClassify());
            //如果有上级id，继续查询
            if (classify.getSuperiorId() != 0) {
                Classify classify1 = this.findClassify(classify.getSuperiorId());
                classifyName.insert(0, classify1.getClassify() + ">");
                //如果有上级id，继续查询
                if (classify1.getSuperiorId() != 0) {
                    Classify classify2 = this.findClassify(classify1.getSuperiorId());
                    classifyName.insert(0, classify2.getClassify() + ">");
                }
            }
        }
        //把specid转换成specname
        List<ProductDetails> list = productDetailsService.findList(id);
        for (ProductDetails productDetails : list) {
            String specId = String.valueOf(productDetails.getSpecId());
            String[] split = specId.split(",");
            String name = "";
            int count = 0;
            for (String s : split) {
                GoodsSpec spec = (GoodsSpec) specService.findById(Long.valueOf(s));
                if (count == 0) {
                    name = name + spec.getSpecValue();
                } else {
                    name = name + " " + spec.getSpecValue();
                }
                count++;
            }
            productDetails.setSpecId(Long.valueOf(name));
        }


        //基本信息
        result.put("productCategory", classifyName);
        result.put("productName", product.getName());
        result.put("productSelling", product.getSelling());
        result.put("productParameter", product.getParameter());
        //商品交易信息
        result.put("unitMeasure", product.getUnitMeasure());
        result.put("salesModel", product.getSalesModel());
        //商品规格及图片
        result.put("productId", product.getId().toString());
        result.put("productPrice", product.getPrice());
        result.put("img", product.getImg());
        result.put("video", product.getVideo());
        Map map = new HashMap();
        map.put("productSpec",list);
        result.put("productSpec", map);
        result.put("productDetailDescription", product.getDescription());
        return result;
    }

    //id查询分类对象
    public Classify findClassify(Long id) {
        Classify classifys = new Classify();
        classifys.setId(id);
        classifys.setIsDelete(0);
        Classify classify = classifyMapper.selectOne(classifys);
        return classify;
    }
}